package com.woniu.cq.medical.controller;


import com.woniu.cq.medical.common.Result;
import com.woniu.cq.medical.controller.form.RegisterForm;
import com.woniu.cq.medical.controller.form.RegisterListForm;
import com.woniu.cq.medical.controller.form.RegisterWindowForm;
import com.woniu.cq.medical.entity.Register;
import com.woniu.cq.medical.service.RegisterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2020-09-05
 */
@RestController
@RequestMapping("/medical/register")
@Slf4j
public class RegisterController {

    @Autowired
    private RegisterService registerService;




    /**
     * 线上新增挂号
     * @param registerForm
     * @return
     */
    @PostMapping("insertRegister")
    public Result insertRegister(@RequestBody RegisterForm registerForm) throws ParseException {
        //未就诊三次 验证
        List<Register> registers = registerService.queryRegisterThree(registerForm.getPatientIdCard(),"未就诊");
        System.out.println(registers);
        if (registers.size()>=3){
            return Result.success("本月三次挂号,而未到我院就诊,账户锁定无法预约,急诊请拨打120");
        }
        //增加一个当天无法重复挂号验证
        List<Register> registersRepetition = registerService.queryRegisterRepetition(registerForm);
        log.info("重复挂号检查："+registersRepetition);
        if (registersRepetition.size()!=0){
            return Result.success("重复挂号");
        }

        //这个地方增加一个缴费验证
        int i = registerService.insertRegister(registerForm);
        if(i>0){
            return Result.success("挂号成功");
        }else {
            return Result.success("挂号失败");
        }
    }

    /**
     * 根据参数查询用户本月内未就诊或其他情况
     * @param patientIdCard
     * @return
     */
    @GetMapping("selectRegisterThree")
    public Result selectRegisterThree(String patientIdCard,String status) throws ParseException {
        List<Register> registers = registerService.queryRegisterThree(patientIdCard,status);
        return Result.success(registers);
    }

    /**
     * 根据身份ID查询挂号信息
     * @param patientIdCard
     * @return
     */
    @GetMapping("selectRegisterList")
    public Result selectRegisterList(String patientIdCard){
        List<RegisterListForm> registerListForms = registerService.queryRegisterList(patientIdCard);
        return Result.success(registerListForms);
    }

    /**
     * 窗口挂号
     * @return
     */
    @PostMapping("insertWindowRegister")
    public Result insertWindowRegister(@RequestBody RegisterWindowForm registerWindowForm) throws ParseException {
        /**
         * 本月之内未就诊三次验证
         */
        List<Register> registers = registerService.queryRegisterThree(registerWindowForm.getPatientIdCard(),"未就诊");
        System.out.println(registers);
        if (registers.size()>=3){
            return Result.success("本月三次挂号,而未到我院就诊,账户锁定无法预约,急诊请拨打120");
        }

        /**
         * 重复挂号验证
         */
        List<Register> registerTwo = registerService.queryRegisterTwo(registerWindowForm.getPatientIdCard(), registerWindowForm.getDutyId());
        log.info("重复挂号检查："+registerTwo);
        if (registerTwo.size()!=0){

            return Result.success("重复挂号");
        }

        /**
         * 窗口新增
         */
        int i = registerService.insertWindowRegister(registerWindowForm);
        if(i>0){
            return Result.success("挂号成功");
        }else {
            return Result.success("挂号失败");
        }
    }

}

